from PyQt5.QtWidgets import *
from PyQt5 import uic

def resource_path(relative_path):
    import sys, os
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.abspath(relative_path)

class SetWindow(QWidget):
    def __init__(self, ourimg, grap, name):
        super().__init__()
        self.ourimg = ourimg
        self.grap = grap
        self.init_ui()
        self.name = name

    def init_ui(self):
        self.ui = uic.loadUi(resource_path(r"img_adjust.ui"))

        #hist组件
        self.grap_hist = self.ui.graphicsView
        self.slider_top_hist = self.ui.verticalSlider_2 # 300 300-0
        self.slider_low_hist = self.ui.verticalSlider_3 # 0 0-300
        self.change_color()
        self.flip_V = self.ui.pushButton_2
        self.flip_H = self.ui.pushButton_3

        self.flip_V.clicked.connect(lambda: self.img_flip(stage=0))
        self.flip_H.clicked.connect(lambda: self.img_flip(stage=1))

        self.show_line = self.ui.lineEdit
        self.show_line.setText("Orignal: %d*%d   Standsize:%.4f"%(self.ourimg.height,
                                                                  self.ourimg.width,
                                                                  800.0/self.ourimg.height))
        #rescale组件
        #self.slider_rescale = self.ui.verticalSlider
        self.line_rescale = self.ui.doubleSpinBox
        self.line_rescale.setValue(self.ourimg.scale)

        # 信号和槽传递参数
        # hist组件
        self.slider_low_hist.sliderReleased.connect(self.change_color)
        self.slider_top_hist.sliderReleased.connect(self.change_color)

        #rotate组件
        #self.dial_rotate.sliderMoved.connect(lambda: self.img_rotate(stage=0))
        #self.line_rotate.editingFinished.connect(lambda: self.img_rotate(stage=1))

        #rescale
        #self.slider_rescale.sliderMoved.connect(lambda: self.img_rescale(stage=0))
        self.line_rescale.editingFinished.connect(lambda: self.img_rescale(stage=1))

        #保存信号
        self.ui.closeEvent = self.closeEvent

    def closeEvent(self,event):
        self.ourimg.save()
        self.ourimg.draw_img(self.grap)
        """print("*"*100)
        reply = QMessageBox.question(self, 'save', '是否要保存数据？',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.ourimg.save()
            self.ourimg.draw_img(self.grap)
            event.accept()
        else:
            event.accept()"""

    def change_color(self):
        self.ourimg.change_color(self.grap, self.grap_hist, self.slider_low_hist.value(), self.slider_top_hist.value())
    """def img_rotate(self,stage):
        if stage==0:
            angle = self.dial_rotate.value()/10
            self.line_rotate.setText(str(angle))
        if stage==1:
            angle = float(self.line_rotate.text())
            self.dial_rotate.setValue(angle*10)
        print(angle)
        self.img.angle = angle
        self.img.rotation(angle, self.grap)"""

    def img_rescale(self,stage):
        """if stage==0:
            rescale = (self.slider_rescale.value()-1000.0)/1000.0+1.0
            self.line_rescale.setText(str(rescale))"""
        if stage==1:
            rescale = self.line_rescale.value()
        self.ourimg.rescale(rescale,self.grap)

    def img_flip(self,stage):
        self.ourimg.flip(stage,self.grap)






